package indexingSystem;

import java.sql.*;

public class IndexingDatabase {

	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = null;
	private static String user = null;
	private static String password = null;
	private static Connection connection = null;
	private static Statement s = null;
	private static IndexingDatabase iface = null;
	
	private IndexingDatabase() throws Exception {
		connect();
	}
	public static void setDriver(String drv) {
		driver = drv;
	}
	public static void setURL(String url_str) {
		url = url_str;
	}
	public static void setUser(String usr) {
		user = usr;
	}
	public static void setPassword(String passwd) {
		password = passwd;
	}
	public static IndexingDatabase getInterface() throws Exception {
		if (iface == null)
			iface = new IndexingDatabase();
		return iface;
	}
	public void connect() throws Exception {
		if(connection == null) {
			Class.forName(driver);	
			connection = DriverManager.getConnection(url, user, password);
			s = connection.createStatement();
		}
	}
	public void close() throws Exception {
		if (s != null) {
			s.close();
			s = null;
		}
		if (connection  != null) {
			connection.close();
			connection = null;
		}
		iface = null;
	}
	public void addWord(int docID, String word) throws Exception {
		ResultSet result = null;
		int wordID;
		int freq;
		result = s.executeQuery("SELECT id, word, frequency FROM word WHERE word='"+word+"'");
		if(result.next()) {
			freq = result.getInt(3)+1;
			wordID = result.getInt(1);
			s.executeUpdate("UPDATE word SET frequency="+freq+" WHERE id="+wordID+"");
		} else {		
			s.execute("INSERT INTO word (word, frequency) VALUES ('"+word+"', 1)", new String[] {"id"} );
			result = s.getGeneratedKeys();
			result.next();
			wordID = result.getInt(1);
		}
		result.close();
		result = s.executeQuery("SELECT word_id, doc_ id, frequency FROM document_word WHERE doc_id="+docID+" and word_id="+wordID+"");
		if(result.next()) {
			freq = result.getInt(3)+1;
			s.executeUpdate("UPDATE document_word SET frequency="+freq+" WHERE word_id='"+wordID+"'");
		} else {
			s.executeUpdate("INSERT INTO document_word (doc_id, word_id, frequency) VALUES ("+docID+","+wordID+", 0)" );
		}
		result.close();
	}

	public int addDocument(String path) throws Exception {
		int docID = getDocumentID(path);
		if (docID != -1)
			return docID;
		s.execute("INSERT INTO document (documentPath) VALUES ('"+path+"') ", new String[] {"id"});
		ResultSet r = s.getGeneratedKeys();
		r.next();
		return r.getInt(1);
	}

	public int getDocumentID(String path) throws Exception {
		ResultSet results = null;
		results = s.executeQuery("SELECT id FROM document WHERE documentPath='"+path+"'");
		if(results.next())
			return results.getInt(1);
		else
			return -1;
	}
	public int wordFreq(String word) throws Exception {
        ResultSet results = null;
        results = s.executeQuery("SELECT frequency FROM word WHERE word=\""+word+"\"");
        if (results.next())
        	return results.getInt(1);
        return -1;
	}
	public int wordDocFreq(int docID, String word) throws Exception {
		ResultSet r = s.executeQuery("SELECT frequency FROM document_word WHERE doc_id='"+docID+"' and word='"+word+"'");
        if (r.next())
        	return r.getInt(1);
        return -1;
	}
	public ResultSet queryDB(String query) throws Exception {
        ResultSet results = null;
        results = s.executeQuery(query);
        if (results.next())
        	return results;
        return null;
	}	
}